<?php
/******************************************************
v4 - Management Framework
Copyright (C) 2008  Gary Taylor, gMerc Incorporation
http://www.gmerc.com/v4/

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

gary@gmerc.com
******************************************************/
class websites {
  function fetch_inputpermission($inp,$inptype='') {
    global $masterclass;
/*
    if ($masterclass->allclasses['authentication']->mysql_check_permission($inp,'field',$masterclass->site['dirpath'],$masterclass->site['page'])=='1') {
      return '';
    } else {
      switch ($inptype) {
       default:
       case 'input_text':
       case 'select':
       case 'textarea':
        return ' readonly ';
      break;
      }
    }
*/

  return;
  }
  function disp_text($sitevar) {
    global $masterclass;
    $ret='';
    $ret.=$masterclass->displayhtmlvalue($masterclass->sitevars[$sitevar],4);

  return $ret;
  }
  
  function disp_inputtext_static($sitevar,$siteval='',$width='90%',$charlength='20',$maxlength='190',$onchange='',$onfocus='',$onkeyup='',$onkeydown='') {
    global $masterclass;
    $ret='';

    $ret.='<input type="text" value="'.$siteval.'" name="'.$sitevar.'" id="'.$sitevar.'" ';
    if ($charlength!='') { $ret.=' size="'.$charlength.'" '; }
    if ($width!='') { $ret.=' style="width:'.$width.';" '; }
    if ($maxlength!='') { $ret.=' maxlength="'.$maxlength.'" '; }
    if ($onchange!='') { $ret.=' OnChange="'.$onchange.'" '; }
    if ($onfocus!='') { $ret.=' OnFocus="'.$onfocus.'" '; }
    if ($onkeyup!='') { $ret.=' OnKeyUp="'.$onkeyup.'" '; }
    if ($onkeydown!='') { $ret.=' OnKeyDown="'.$onkeydown.'" '; }
    $ret.=' align="absmiddle" '.$this->fetch_inputpermission($sitevar,'input_text').'>';
  return $ret;
  }  
  function disp_select_static($options,$sitevar,$siteval='',$width='90%',$onchange='') {
    $ret='';
    $usedefault=" selected ";

    if ($sitevar!='' && !empty($options)) {
      if (isset($masterclass->sitevars[$sitevar])) {
        foreach ($options as $option) { if ($siteval==$option[0]) { $usedefault=''; } }
      }
      $ret.='<select name="'.$sitevar.'" id="'.$sitevar.'" ';
      if ($width!='') { $ret.=' style="width:'.$width.';" '; }
      $ret.=' align="absmiddle" ';
      if ($onchange!='') { $ret.=' OnChange="'.$onchange.'" '; }
      $ret.=$this->fetch_inputpermission($sitevar,'select');
      $ret.='>';
      foreach ($options as $option) {
        if (!isset($option[1])) { $optvalue=$option[0];$optdisplay=$optvalue; } else { $optvalue=$option[0];$optdisplay=$option[1]; }
        $ret.='<option value="'.$optvalue.'"';
        if (isset($siteval) && $siteval==$optvalue) { $ret.=' selected '; } else
        if ($optvalue==$default) { $ret.=$usedefault; }
        $ret.='>'.$optdisplay.'</option>';
      }
      $ret.='</select>';
    }
  return $ret;
  }
  function disp_inputcheckbox_static($sitevar,$siteval='',$checkedvalue='',$defaultcheck='') {
    global $masterclass;
    $ret='';

    $ret.='<input type="checkbox" value="'.$checkedvalue.'" name="'.$sitevar.'" id="'.$sitevar.'" align="absmiddle" ';
    if ($siteval==$checkedvalue || (!isset($siteval) && $defaultcheck!='')) { $ret.=' checked '; }
    $ret.=' '.$this->fetch_inputpermission($sitevar,'input_text').'>';
  return $ret;
  }
  function disp_inputradio_static($sitevar,$siteval='',$checkedvalue='',$defaultcheck='') {
    global $masterclass;
    $ret='';

    $ret.='<input type="radio" value="'.$checkedvalue.'" name="'.$sitevar.'" id="'.$sitevar.'" align="absmiddle" ';
    if ($siteval==$checkedvalue || (!isset($siteval) && $defaultcheck!='')) { $ret.=' checked '; }
    $ret.=' '.$this->fetch_inputpermission($sitevar,'input_text').'>';
  return $ret;
  }
  function disp_textarea_static($sitevar,$siteval='',$width='100%',$tarows='3',$tacols='20') {
    global $masterclass;
    $ret='';

    $ret='<textarea name="'.$sitevar.'" id="'.$sitevar.'" cols="'.$tacols.'" rows="'.$tarows.'" align="absmiddle" style="width:'.$width.';" '.$this->fetch_inputpermission($sitevar,'textarea').'>'.$siteval.'</textarea>';
  return $ret;
  }
  
  
  
  
  function disp_select($options,$sitevar,$default='',$width='90%',$onchange='') {
    $ret='';
    $usedefault=" selected ";

    if ($sitevar!='' && !empty($options)) {
      if (isset($masterclass->sitevars[$sitevar])) {
        foreach ($options as $option) { if ($masterclass->sitevars[$sitevar]==$option[0]) { $usedefault=''; } }
      }
      $ret.='<select name="'.$sitevar.'" id="'.$sitevar.'" ';
      if ($width!='') { $ret.=' style="width:'.$width.';" '; }
      $ret.=' align="absmiddle" ';
      if ($onchange!='') { $ret.=' OnChange="'.$onchange.'" '; }
      $ret.=$this->fetch_inputpermission($sitevar,'select');
      $ret.='>';
      foreach ($options as $option) {
        if (!isset($option[1])) { $optvalue=$option[0];$optdisplay=$optvalue; } else { $optvalue=$option[0];$optdisplay=$option[1]; }
        $ret.='<option value="'.$optvalue.'"';
        if (isset($masterclass->sitevars[$sitevar]) && $masterclass->sitevars[$sitevar]==$optvalue) { $ret.=' selected '; } else
        if ($optvalue==$default) { $ret.=$usedefault; }
        $ret.='>'.$optdisplay.'</option>';
      }
      $ret.='</select>';
    }
  return $ret;
  }
  function disp_inputtext($sitevar,$width='90%',$charlength='20',$maxlength='190',$onchange='',$onfocus='',$onkeyup='',$onkeydown='') {
    global $masterclass;
    $ret='';

    $ret.='<input type="text" value="'.$masterclass->displayhtmlvalue($masterclass->sitevars[$sitevar]).'" name="'.$sitevar.'" id="'.$sitevar.'" ';
    if ($charlength!='') { $ret.=' size="'.$charlength.'" '; }
    if ($width!='') { $ret.=' style="width:'.$width.';" '; }
    if ($maxlength!='') { $ret.=' maxlength="'.$maxlength.'" '; }
    if ($onchange!='') { $ret.=' OnChange="'.$onchange.'" '; }
    if ($onfocus!='') { $ret.=' OnFocus="'.$onfocus.'" '; }
    if ($onkeyup!='') { $ret.=' OnKeyUp="'.$onkeyup.'" '; }
    if ($onkeydown!='') { $ret.=' OnKeyDown="'.$onkeydown.'" '; }
    $ret.=' align="absmiddle" '.$this->fetch_inputpermission($sitevar,'input_text').'>';
  return $ret;
  }

  function disp_inputpassword($sitevar,$width='90%',$charlength='20',$maxlength='190') {
    global $masterclass;
    $ret='';

    $ret.='<input type="password" value="'.$masterclass->displayhtmlvalue($masterclass->sitevars[$sitevar]).'" name="'.$sitevar.'" id="'.$sitevar.'" ';
    if ($charlength!='') { $ret.=' size="'.$charlength.'" '; }
    if ($width!='') { $ret.=' style="width:'.$width.';" '; }
    if ($maxlength!='') { $ret.=' maxlength="'.$maxlength.'" '; }
    $ret.=' align="absmiddle" '.$this->fetch_inputpermission($sitevar,'input_text').'>';
  return $ret;
  }
  function disp_inputcheckbox($sitevar,$checkedvalue='',$defaultcheck='') {
    global $masterclass;
    $ret='';

    $ret.='<input type="checkbox" value="'.$checkedvalue.'" name="'.$sitevar.'" id="'.$sitevar.'" align="absmiddle" ';
    if ($masterclass->sitevars[$sitevar]==$checkedvalue || (!isset($masterclass->sitevars[$sitevar]) && $defaultcheck!='')) { $ret.=' checked '; }
    $ret.=' '.$this->fetch_inputpermission($sitevar,'input_text').'>';
  return $ret;
  }
  function disp_inputradio($sitevar,$checkedvalue='',$defaultcheck='') {
    global $masterclass;
    $ret='';

    $ret.='<input type="radio" value="'.$checkedvalue.'" name="'.$sitevar.'" id="'.$sitevar.'" align="absmiddle" ';
    if ($masterclass->sitevars[$sitevar]==$checkedvalue || (!isset($masterclass->sitevars[$sitevar]) && $defaultcheck!='')) { $ret.=' checked '; }
    $ret.=' '.$this->fetch_inputpermission($sitevar,'input_text').'>';
  return $ret;
  }
  function disp_textarea($sitevar,$width='100%',$tarows='3',$tacols='20') {
    global $masterclass;
    $ret='';

    $ret='<textarea name="'.$sitevar.'" id="'.$sitevar.'" cols="'.$tacols.'" rows="'.$tarows.'" align="absmiddle" style="width:'.$width.';" '.$this->fetch_inputpermission($sitevar,'textarea').'>'.$masterclass->displayhtmlvalue($masterclass->sitevars[$sitevar],4).'</textarea>';
  return $ret;
  }
  function disp_pages($runcmdpre,$runcmdpost,$unquieid,$pg=1,$pgcnt=1) {
    global $masterclass;

    if ($pgcnt<1) { $pgcnt=1; }
    if ($pg<1 || $pg>$pgcnt) { $pg=1; }
    $ret='';
    $ret.='<table width="100%" cellpadding="1" cellspacing="0" border="0"><tr class="sitebox_navigation">
    <td align="left" style="border-bottom:1px solid;"><nobr>Page: <b><span id="pagelistpg'.$unquieid.'">'.$pg.'</span></b> of <span id="pagelistcnt'.$unquieid.'">'.$pgcnt.'</span></nobr>&nbsp;:&nbsp;<nobr>Goto Page: <select name="pagelist'.$unquieid.'" id="pagelist'.$unquieid.'" OnChange="';
    if ($runcmdpre!='') { $ret.=$runcmdpre.$p; }
    $ret.=$runcmdpost.'"><option value="1">1</option>';
    for ($p=2;$p<=$pgcnt;$p++) { $ret.='<option value="'.$p.'"';if ($p==$pg) { $ret.=' selected '; } $ret.='>'.$p.'</option>'; }
    $ret.='</select>';
    $ret.='</nobr></td></tr></table>';

  return $ret;
  }

  function load_tracking_inputs() {
    global $masterclass;

    $ret='';

    $arrtrackingid = $masterclass->loadtrackingfields();

    if (!empty($arrtrackingid)) {
      foreach ($arrtrackingid as $trackingvar) {
        $ret.='<input type="hidden" name="'.$trackingvar.'" id="'.$trackingvar.'" value="'.$masterclass->sitevars[$trackingvar].'">';$ret.="\n";
      }
    }

  return $ret;
  }

  function siteheader() {
    global $masterclass;

    $ret='';

    $ret.=$this->add_html();
    $ret.=$this->add_meta();
    $ret.="<head>\n";
    $ret.=$this->add_title();
    $ret.=$this->add_meta();
    $ret.=$this->add_css();
    $ret.=$this->add_js();
    $ret.="</head>\n";
    $ret.=$this->add_body();
    return $ret;
  }

  function sitefooter() {
    $ret.='';

    $ret.="</body>\n";
    $ret.="</html>";

    return $ret;
  }
  function add_html() {
    $ret='';
    $ret.="<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n";
    $ret.="<html>\n";
  return $ret;
  }
  function add_title() {
    global $masterclass;

    $ret='';
    $ret.="  <title>".$masterclass->websiteconfig['title']."</title>\n";
  return $ret;
  }
  function add_meta() {
    global $masterclass;

    $ret='';
    $ret.="<meta name=\"description\" content=\"".$masterclass->websiteconfig['meta_desc']."\">\n";
    $ret.="<meta name=\"keywords\" content=\"".$masterclass->websiteconfig['meta_keys']."\">\n";
  return $ret;
  }
  function add_css() {
    global $masterclass;

    $ret='';
    if (!empty($masterclass->websiteconfig['css_include'])) {
      foreach ($masterclass->websiteconfig['css_include'] as $inc) {
        $ret.="  <link href=\"".$masterclass->mainconfig['dir'].$inc."\" rel=\"stylesheet\" type=\"text/css\">\n";
      }
    }
  return $ret;
  }
  function add_js() {
    global $masterclass;

    $ret='';
    if (!empty($masterclass->websiteconfig['javascript_include'])) {
      foreach ($masterclass->websiteconfig['javascript_include'] as $inc) {
        //$ret.=" <script language="\"javascript\" src=\"".$masterclass->mainconfig['dir']."include/jsinc.php?inc=".$inc."\">\n";
        $ret.="  <script language=\"javascript\" src=\"".$masterclass->mainconfig['dir'].$inc."\"></script>\n";
      }
    }
    if ($masterclass->websiteconfig['javascript_static']!='') {
      require $masterclass->mainconfig['v4rootdir'].'lib/jsmin-1.1.1.php';
      $ret.="<script language=\"javascript\">\n";
      $ret.=JSMin::minify($masterclass->websiteconfig['javascript_static'])."\n";
      $ret.="</script>\n";
    }
  return $ret;
  }
  function add_body() {
    global $masterclass;

    $ret='';
    $ret.="<body bgcolor=\"".$masterclass->websiteconfig['body_bgcolor']."\" text=\"".$masterclass->websiteconfig['body_text']."\" link=\"".$masterclass->websiteconfig['body_link']."\" vlink=\"".$masterclass->websiteconfig['body_vlink']."\" alink=\"".$masterclass->websiteconfig['body_alink']."\" topmargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" onLoad=\"".$masterclass->websiteconfig['body_onload']."\">\n";
  return $ret;
  }
  function add_login($loginonclick='') {
    global $masterclass;

    if ($loginonclick=='') { $loginonclick=' frm.submit(); '; }
    $loginbutton='<input type="button" OnClick="'.$loginonclick.'" value=" Login ">';
    $ret='';
    $ret.='<table cellpadding="2" cellspacing="1" border="0"><tr valign="middle"><td align="center" style="text-align:right;">Login: </td><td style="text-align:left;"><input type="text" name="login_username" id="login_username" size="15" OnKeyPress="if (event.keyCode == 13) { document.getElementById(\'login_password\').focus(); }"></td></tr><tr valign="middle"><td align="center" style="text-align:right;">Password: </td><td style="text-align:left;"><input type="password" name="login_password" id="login_password" size="15" OnKeyPress="if (event.keyCode == 13) { '.$loginonclick.' }"></td></tr><tr valign="middle"><td align="center" style=text-align:center;" colspan="2">'.$loginbutton.'</td></tr></table>';
    $ret.='<input type="hidden" name="login_authid" value="'.$_SESSION['login_authid'].'">';
  return $ret;
  }
}